package cn.kpi.service.sys.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.sys.RightsDao;
import cn.kpi.dao.sys.UserDao;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.service.sys.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RightsDao rightsDao;
	
	public UserEntity UserLogin()throws Exception{
		try
		{
			return userDao.UserLogin();
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	@Transactional
	public int InsertEntity(UserEntity entity)throws Exception{
		try
		{
			int result=0;
			result+=userDao.InsertEntity(entity);
			Long userid=entity.getUserid();
			//result+=rightsDao.DeleteRoleUserByUserid(entity.getRoleid(), userid);
			result+=rightsDao.DeleteAllRoleUserByUserid(userid);
			result+=rightsDao.InsertRoleUser(userid, entity.getRoleid());
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	public List<UserEntity> GetUserListByWhere(String where)throws Exception{
		try
		{
			return userDao.GetUserListByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<UserEntity> GetUserListTranByWhere(String where,String year ,String month) throws Exception {
		try
		{
			return userDao.GetUserListTranByWhere(where,year,month);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

    @Override
    public List<UserEntity> GetUserListWorkByWhere(String where, String year, String month) throws Exception {
		try
		{
			return userDao.GetUserListWorkByWhere(where,year,month);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
    }

    public int GetUserListCountByWhere(String where)throws Exception{
		try
		{
			return userDao.GetUserListCountByWhere(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	 public int DeleteEntity(Long userid)throws Exception
	 {
		 try
			{
				return userDao.DeleteEntity(userid);
			}
			catch(Exception ex){
				ex.printStackTrace();
				return -1;
			}
	 }
	 public UserEntity GetUserById(Long userid)throws Exception{
		 try
			{
				return userDao.GetUserById(userid);
			}
			catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
	 }
	 @Transactional
	 public int UpdateEntity(UserEntity entity)throws Exception{
		 try
			{
			 int result=0;
			 	UserEntity user = userDao.GetUserById(entity.getUserid());
			 	//判断是否是EXCLE导入的
			 	if(user.getUsername()==null||user.getUsername().equals(""))
				{
					result += userDao.UpdateAllEntity(entity);
				}
			 	else {
					result += userDao.UpdateEntity(entity);
				}
				Long userid=entity.getUserid();
				//result+=rightsDao.DeleteRoleUserByUserid(entity.getRoleid(), userid);
				result+=rightsDao.DeleteAllRoleUserByUserid(userid);
				result+=rightsDao.InsertRoleUser(userid, entity.getRoleid());
				return result;
			}
			catch(Exception ex){
				ex.printStackTrace();
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
	 }

	@Override
	public int UpdateEntityList(List<UserEntity> inentitys,List<UserEntity> upentitys) throws Exception {
		try
		{
			int result=0;
            for (UserEntity upentity : upentitys) {
                result+=userDao.UpdateEntityNullP(upentity);
            }
            for (UserEntity inentity : inentitys) {
                result+=userDao.InsertEntity(inentity);
            }
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}


	public List<UserEntity> GetUserListLimitByWhere(String where,int beginrow,int endrow)throws Exception{
		 try
			{
				return userDao.GetUserListLimitByWhere(where,beginrow,endrow);
			}
			catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
	 }
}
